/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.popup;

import java.util.HashSet;
import java.util.Set;

import com.daikit.daikit4gxt.client.DkBaseMain;
import com.daikit.daikit4gxt.client.ui.UIInvalidatable;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutPack;
import com.sencha.gxt.widget.core.client.event.SelectEvent;


/**
 * Simple popUp with toolBar and some custom control buttons.
 *
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public abstract class DkSimpleControlsPopup extends AbstractDkWindow implements UIInvalidatable
{

	protected ScheduledCommand invalidateUiCommand = new ScheduledCommand()
	{
		@Override
		public void execute()
		{
			invalidateUi();
		}
	};

	private final String SAVE = "save";
	private final String RESET = "reset";
	private final String CANCEL = "cancel";
	private final String SEARCH = "search";
	private final String OK = "ok";

	protected TextButton buttonCancel;
	protected TextButton buttonSave;
	protected TextButton buttonOk;
	protected TextButton buttonSearch;
	protected TextButton buttonReset;

	/**
	 * An enum of all types of buttons supported by this Form Dialog
	 */
	public static enum DisplayableButton
	{
		/**
		 * For save purpose
		 */
		SAVE,
		/**
		 * For OK
		 */
		OK,
		/**
		 * For reseting the form
		 */
		RESET,
		/**
		 * For canceling, closes the dialog box
		 */
		CANCEL,
		/**
		 * For searching
		 */
		SEARCH
	}

	protected final Set<DisplayableButton> displayableButtons = new HashSet<DkSimpleControlsPopup.DisplayableButton>();


	/**
	 * Constructor
	 *
	 * @param resizable
	 * @param displayableButtons
	 */
	public DkSimpleControlsPopup(final boolean resizable, final DisplayableButton... displayableButtons)
	{
		setModal(true);
		setHeadingText(getLabelPopupTitle());
		setWidth(DkBaseMain.config().getPopupWidth());
		setButtonAlign(BoxLayoutPack.CENTER);
		setResizable(resizable);

		for (final DisplayableButton displayableButton : displayableButtons)
		{
			this.displayableButtons.add(displayableButton);
		}

		if (this.displayableButtons.contains(DisplayableButton.CANCEL))
		{
			buttonCancel = new TextButton(getCancelButtonLabel(), new SelectEvent.SelectHandler()
			{
				@Override
				public void onSelect(final SelectEvent event)
				{
					DkSimpleControlsPopup.this.onButtonCancelClicked();
				}
			});
			buttonCancel.setItemId(CANCEL);
			buttonCancel.setIcon(DkBaseMain.icons().cancel_16());
			addButton(buttonCancel);
		}

		if (this.displayableButtons.contains(DisplayableButton.RESET))
		{
			buttonReset = new TextButton(getResetButtonLabel(), new SelectEvent.SelectHandler()
			{

				@Override
				public void onSelect(final SelectEvent event)
				{
					DkSimpleControlsPopup.this.onButtonResetClicked();
				}

			});
			buttonReset.setItemId(RESET);
			buttonReset.setIcon(DkBaseMain.icons().refresh_red_16());
			addButton(buttonReset);
		}

		if (this.displayableButtons.contains(DisplayableButton.SAVE))
		{
			buttonSave = new TextButton(getSaveButtonLabel(), new SelectEvent.SelectHandler()
			{
				@Override
				public void onSelect(final SelectEvent event)
				{
					DkSimpleControlsPopup.this.onButtonSaveClicked();
				}
			});
			buttonSave.setItemId(SAVE);
			buttonSave.setIcon(DkBaseMain.icons().save_16());
			addButton(buttonSave);
		}

		if (this.displayableButtons.contains(DisplayableButton.OK))
		{
			buttonOk = new TextButton(getOkButtonLabel(), new SelectEvent.SelectHandler()
			{
				@Override
				public void onSelect(final SelectEvent event)
				{
					DkSimpleControlsPopup.this.onButtonOkClicked();
				}
			});
			buttonOk.setItemId(OK);
			buttonOk.setIcon(DkBaseMain.icons().ok_16());
			addButton(buttonOk);
		}

		else if (this.displayableButtons.contains(DisplayableButton.SEARCH))
		{
			buttonSearch = new TextButton(getSearchButtonLabel(), new SelectEvent.SelectHandler()
			{
				@Override
				public void onSelect(final SelectEvent event)
				{
					DkSimpleControlsPopup.this.onButtonSearchClicked();
				}
			});
			buttonSearch.setItemId(SEARCH);
			buttonSearch.setIcon(DkBaseMain.icons().file_explore_16());
			addButton(buttonSearch);
		}

		setFocusWidget(buttonSave);
	}

	@Override
	public void show()
	{
		super.show();
		Scheduler.get().scheduleFinally(invalidateUiCommand);
	}

	/**
	 * InvalidateUi method
	 */
	@Override
	public void invalidateUi()
	{
		if (buttonSave != null)
		{
			buttonSave.setEnabled(DkBaseMain.model().isLogged());
		}
		if (buttonOk != null)
		{
			buttonOk.setEnabled(DkBaseMain.model().isLogged());
		}
		if (buttonCancel != null)
		{
			buttonCancel.setEnabled(true);
		}
		if (buttonReset != null)
		{
			buttonReset.setEnabled(true);
		}
		if (buttonSearch != null)
		{
			buttonSearch.setEnabled(DkBaseMain.model().isLogged());
		}
	}

	protected void onButtonSaveClicked()
	{

	}

	protected void onButtonOkClicked()
	{

	}

	protected void onButtonSearchClicked()
	{

	}

	protected void onButtonResetClicked()
	{

	}

	protected void onButtonCancelClicked()
	{
		hide();
	}

	protected String getSaveButtonLabel()
	{
		return DkBaseMain.i18n().label_save();
	}

	protected String getOkButtonLabel()
	{
		return DkBaseMain.i18n().label_ok();
	}

	protected String getResetButtonLabel()
	{
		return DkBaseMain.i18n().label_reset();
	}

	protected String getCancelButtonLabel()
	{
		return DkBaseMain.i18n().label_cancel();
	}

	protected String getSearchButtonLabel()
	{
		return DkBaseMain.i18n().label_search();
	}

	protected abstract String getLabelPopupTitle();
}
